<%@ include file="../../../base/include.jsp"%>

<security:authentication property="principal.id" var="currentUserId" />

<div class="page-header">
	<h1>Logged in Users</h1>
</div>

<form:form method="post" commandName="loggedUsersCommand">
	<table class="table table-striped table-condensed black">
		<thead>
			<tr>
				<th>Name</th>
				<th>Logging time, Browser, IP address</th>
				<th width="75px"></th>
			</tr>
		</thead>
		<tbody>
			<c:forEach var="loggedUser" items="${loggedUsersCommand.loggedUsers}"
				varStatus="iterator">
				<tr>
					<td><a href="<c:url value='/profile/${loggedUser.id}'/>">${loggedUser.name}</a></td>
					<td>${loggedUser.info}</td>
					<td><c:if test="${currentUserId != loggedUser.id}">
							<span class="badge badge-info pointerCursor"
								onclick="showMessage(${loggedUser.id}, 'delete');"
								title="Disconnect user"> <i
								class="icon-minus-sign icon-white"></i>
							</span>
						</c:if></td>
				</tr>
			</c:forEach>
		</tbody>
	</table>
</form:form>

<div id="disconnectModal" class="modal fade in" style="display: none;">
	<div class="modal-header" style="border-bottom: 0px;">
		<a href="#" class="close" data-dismiss="modal">&times;</a>
		<h3>Confirm</h3>
	</div>
	<div class="modal-body">
		<div>
			<p>
				<strong id="message"></strong>
			</p>
		</div>
	</div>
	<div class="modal-footer">
		<div class="alert-actions">
			<a id="btnNo" class="btn btn-primary" href="#" data-dismiss="modal">No</a>
			<a id="btnYes" class="btn" href="#">Yes</a>
		</div>
	</div>
</div>

<script type="text/javascript">
	setActiveMainMenuElem('administration');
	$('#menuLoggedUsers').addClass('active');
	
	function showMessage(id, cmnd) {
		if (cmnd == "delete") {
			if (id == null) {
				document.getElementById('message').innerHTML = "Do you want to disconnect all users?";
				document.getElementById('btnYes').setAttribute('href', '<c:url value="/admin/user/disconnectAll"/>');
			} else {
				document.getElementById('message').innerHTML = "Do you want to disconnect this user?";
				document.getElementById('btnYes').setAttribute('href', '<c:url value="/admin/user/disconnect/' + id +'"/>');
			}
		} 

		$('#disconnectModal').modal({
			backdrop: true,	
			keyboard: true,
			show: true
		});
	};
</script>
